package apogeu.seminario.models.negocio;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import apogeu.monitora.models.Sessao;
import apogeu.seminario.models.dados.Inscricao;

public class NegocioInscricao {
	
	public boolean inserir(Inscricao inscricao) {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			
			session.save(inscricao);
			
			transaction.commit();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		return false;
	}
	
	public Inscricao get(int idInscricao) {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			
			Criteria criteria = session.createCriteria(Inscricao.class);
			criteria.add(Restrictions.idEq(idInscricao));
			criteria.setMaxResults(1);
			
			List<Inscricao> lista = criteria.list();
			
			if (lista.size() > 0) {
				return lista.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		return null;
	}
	
	public List<Inscricao> paginacao(int posicao, int maximo) {
		SessionFactory sessionFactory = null;
		Session session = null;
		int paginacao = posicao * maximo;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			
			Criteria criteria = session.createCriteria(Inscricao.class);
			
			criteria.setFirstResult(paginacao);
			criteria.setMaxResults(maximo);
			
			return criteria.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		return null;
	}
	
	public List<Inscricao> paginacao(int posicao, int maximo, Date data) {
		SessionFactory sessionFactory = null;
		Session session = null;
		int paginacao = posicao * maximo;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			
			Criteria criteria = session.createCriteria(Inscricao.class);
			criteria.add(Restrictions.ge("data", data));
			criteria.setFirstResult(paginacao);
			criteria.setMaxResults(maximo);
			
			return criteria.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		return null;
	}
	
	public int quantidade() {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			
			Criteria criteria = session.createCriteria(Inscricao.class);
			
			return criteria.list().size();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		return 0;
	}
	
	public static void main(String[] args) {
		NegocioInscricao negocioInscricao = new NegocioInscricao();
		List<Inscricao> lista = negocioInscricao.paginacao(0, 10, new Date());
		
		for (Inscricao inscricao : lista) {
			System.out.println(inscricao.getIdInscricao() + "  " + inscricao.getNome());
		}
	}
}
